<?php
class convenios  extends CI_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Convenios_Model');
		//$this->load->model('');
	}


	function index($offset = 0)
	{

		$this->load->library('pagination');
	  
		$perpage = 5;
		$config['base_url'] = site_url() . '/convenios/index/';
		$config['total_rows'] = $this->Convenios_Model->Listar(array('count' => true));
		$config['per_page'] = $perpage;
		$config['uri_segment'] = 3;
		$config['full_tag_open'] = '<div align="center" style="height:10px; padding:10px;">';
		$config['full_tag_close'] = '</div>';
	  
		$this->pagination->initialize($config);
	  
		$content['pagination'] = $this->pagination->create_links();
		$content['filas'] = $this->Convenios_Model->Listar(array('limit' => $perpage, 'offset' => $offset));

		$data['title'] = 'LISTADO CONVENIOS';
		$data['content'] = $this->load->view('Convenios/Listar', $content, TRUE);
		$this->load->view('Plantilla/index', $data);

	}

	function ver($id){

		$content['filas'] = $this->Convenios_Model->Ver(array('id' => $id));
		$content['link_back'] =  anchor('convenios/index/','VOLVER AL LISTADO',array('class'=>'atras'));

		$data['title'] = 'DETALLE CONVENIO';
		$data['content'] = $this->load->view('Convenios/Detalle', $content, TRUE);

		$this->load->view('Plantilla/index', $data);
			
	}

	function eliminar($id){

		$content['filas'] = $this->Convenios_Model->Eliminar(array('Id' => $id));
		redirect('convenios/index/','refresh');
		//$this->load->view('Plantilla/index', $data);
			
	}

	function editar($id){

		$content['filas'] = $this->Convenios_Model->VerEditarPersonas(array('id' => $id));
		$content['link_back'] =  anchor('personas/index/','VOLVER AL LISTADO',array('class'=>'atras'));


		$this->form_validation->set_rules('id_rol','Rol Persona','required|trim');
		$this->form_validation->set_rules('nombre','Nombre','required|trim|max_length[30]');
		$this->form_validation->set_rules('apellidos','Apellidos','required|trim|max_length[30]');
		$this->form_validation->set_rules('sexo','Sexo','required|trim|max_length[10]');
		$this->form_validation->set_rules('fnacimiento','Fecha de Nacimiento','required|trim|max_length[20]');
		$this->form_validation->set_rules('direccion','Direccion','trim|max_length[100]');
		$this->form_validation->set_rules('ciudad','Ciudad','required|trim|max_length[50]');
		$this->form_validation->set_rules('telefono','No Telefono','trim|is_numeric|max_length[15]');
		$this->form_validation->set_rules('correo','Correo Electronico','required|trim|valid_email|max_length[50]');
			
		$this->form_validation->set_error_delimiters('<span style="color:red;" class="error">', '</span>');
	  



		if($this->form_validation->run())
		{
			// Validation passes
			 
			 
			$_POST['id'] = $id;
			 
			//if(empty($_POST['userPassword'])) unset($_POST['userPassword']);
			 
			if($this->Convenios_Model->ActualizarPersona($_POST)==1)
			{

				//echo "Error";
				$this->session->set_flashdata('flashConfirm', 'The user has been successfully updated.');
				redirect('personas');
			}
			else
			{
				//echo "Succ";
				$this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
				redirect('personas');
			}
		}



		$data['title'] = 'EDITAR PERSONA: '.$id;
		$data['content'] = $this->load->view('Personas/Editar_Form', $content, TRUE);
		$data['footer'] = 'Sistema de Informacion para la Internacionalizacion de la Universidad del Magdalena - SIADI';

		$this->load->view('Plantilla/index', $data);
			
	}


	function insertar(){

		$content['link_back'] =  anchor('personas/index/','VOLVER AL LISTADO',array('class'=>'atras'));

		$this->form_validation->set_rules('id','No Identificacion','required|is_numeric|callback_id_check[id]|max_length[20]');
		$this->form_validation->set_rules('id_tipo','Tipo de Identificacion','required|trim|max_length[2]');
		$this->form_validation->set_rules('id_rol','Rol Persona','required|trim');
		$this->form_validation->set_rules('nombre','Nombre','required|trim|max_length[30]');
		$this->form_validation->set_rules('apellidos','Apellidos','required|trim|max_length[30]');
		$this->form_validation->set_rules('sexo','Sexo','required|trim|max_length[10]');
		$this->form_validation->set_rules('fnacimiento','Fecha de Nacimiento','required|trim|max_length[20]');
		$this->form_validation->set_rules('direccion','Direccion','trim|max_length[100]');
		$this->form_validation->set_rules('ciudad','Ciudad','required|trim|max_length[50]');
		$this->form_validation->set_rules('telefono','No Telefono','trim|is_numeric|max_length[10]');
		$this->form_validation->set_rules('correo','Correo Electronico','required|trim|valid_email|max_length[50]');
			
		$this->form_validation->set_error_delimiters('<span style="color:red;" class="error">', '</span>');
	  
		if($this->form_validation->run())
		{
			unset($_POST['submit']);
	   
			if($this->Convenios_Model->InsertarPersona($_POST))
			{
				 

				 
				$this->session->set_flashdata('flashConfirm', 'The user has been successfully updated.');
				 
				 
				 
				redirect('personas');

			}
			else
			{
				$this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
				//redirect('practicas');
				echo "estoy dentro de la funcion de resultado fracaso";
			}
		}


		$data['title'] = 'INSERTAR PERSONA';
		$data['content'] = $this->load->view('Personas/Insertar_Form', $content, TRUE);
		$data['footer'] = 'Sistema de Informacion para la Internacionalizacion de la Universidad del Magdalena - SIADI';

		$this->load->view('Plantilla/index', $data);
			
	}

	function id_check($str) {

			
		if ( $this->Convenios_Model->VerPersonas(array('id' => $str,'count' => true)) >  0) {
			$this->form_validation->set_message('id_check', 'El %s ya esta registrado ');
			return FALSE;
		}
		else {
			return TRUE;
		}

	}

}
/* End of file convenios.php */
/* Location: ./application/controllers/convenios.php */